#include "stdafx.h"
#include "XamlSchemaContext.h"

#include "ParserErrorService.h"
#include "XamlNodeStreamCacheManager.h"
#include "CCoreServices.h"
#include "XamlAssembly.h"
#include "XamlXmlNamespace.h"

#include "XamlNativeTypeInfoProvider.h"
#include "XamlManagedTypeInfoProvider.h"
#include "XamlNativeRuntime.h"
#include "XamlManagedRuntime.h"

#include "XamlType.h"

#include "XamlNamespace.h"

extern void XcpMarkWeakPointer(void * thisPtr, void * ptrToWeakPtr);

XamlSchemaContext::XamlSchemaContext() :
	m_spXaml2006Ns(),
	m_spNativeTypeInfoProvider(),
	m_spManagedTypeInfoProvider(),
	m_spNativeRuntime(),
	m_spManagedRuntime(),
	m_spStringType(),
	m_spInt32Type(),
	m_spStringSyntax(),
	m_spInt32Syntax(),
	m_spPositionalParametersProperty(),
	m_spInitializationProperty(),
	m_spItemsProperty(),
	m_SourceAssemblies(),
	m_mapUriToXmlNamespace(),
	m_mapFullyQualifiedProperty(),
	m_mapMasterTypeList(0x100),
	m_mapMasterMemberList(0x100),
	m_mapMasterAssemblyList(0x20),
	m_mapMasterTypeNamespaceList(0x40),
	m_typeVector(),
	m_memberVector(),
	m_xmlNamespaceVector(),
	m_pCore(NULL),
	m_ErrorService(),
	m_bAllowXmlnsOnlyReset(FALSE)

{

	XcpMarkWeakPointer(this,this->m_pCore);

}

HRESULT XamlSchemaContext::Create(CCoreServices * pCore, XBOOL bAllowXmlnsOnlyReset, const xshared_ptr<ParserErrorReporter> & inErrorReporter, xshared_ptr<XamlSchemaContext> & outXamlSchemaContext)
{

	HRESULT hr = S_OK;

	XASSERT_SUCCESSED(outXamlSchemaContext.init(new XamlSchemaContext()));
	outXamlSchemaContext->m_pCore = pCore;

	if(XUINT32(inErrorReporter))
	{
		outXamlSchemaContext->m_ErrorService = inErrorReporter;
	}
	else
	{
		xshared_ptr<ParserErrorService> errorService;
		XASSERT_SUCCESSED(errorService.init(new ParserErrorService()));
		errorService->Initialize(pCore);

		outXamlSchemaContext->m_ErrorService = errorService;
	}


	XASSERT_SUCCESSED(outXamlSchemaContext->InitializeXmlNamespaceMap(FALSE ));
	
	outXamlSchemaContext->m_bAllowXmlnsOnlyReset = bAllowXmlnsOnlyReset;
	return hr;

}

HRESULT XamlSchemaContext::Create(CCoreServices * pCore, XBOOL bAllowXmlnsOnlyReset, xshared_ptr<XamlSchemaContext> & outXamlSchemaContext)
{

	xshared_ptr<ParserErrorReporter> nullErrorReporter;

	return XamlSchemaContext::Create(pCore,bAllowXmlnsOnlyReset,nullErrorReporter,outXamlSchemaContext);

}

HRESULT XamlSchemaContext::ResetXmlnsDefinitions()
{
	HRESULT hr = S_OK;

	XASSERT_NOTNULL_WITH_HR(this->m_bAllowXmlnsOnlyReset,0x8000FFFFu);

	XASSERT_SUCCESSED(this->InitializeXmlNamespaceMap(TRUE ));
	XASSERT_SUCCESSED(this->FlushXamlNodeStreamCacheManager());
	return hr;

}

HRESULT XamlSchemaContext::AddAssembly(struct XamlAssemblyToken inAssemblyToken, const CXString * pstrAssemblyName)
{
	xshared_ptr<XamlSchemaContext> schemaContext;
	xshared_ptr<XamlAssembly> spAssembly;
	xshared_ptr<CXString> spAssemblyName;

	HRESULT hr = S_OK;


	XASSERT_SUCCESSED(shared_from_this(schemaContext));

	XASSERT_SUCCESSED(spAssemblyName.init(pstrAssemblyName->Clone(TRUE,FALSE)));


	if(this->m_bAllowXmlnsOnlyReset)
	{
		XASSERT_SUCCESSED(GetXamlAssembly(inAssemblyToken, spAssemblyName, spAssembly));
	}
	else
	{
		XASSERT_SUCCESSED(XamlAssembly::Create(schemaContext, spAssemblyName, inAssemblyToken, spAssembly));
		XASSERT_SUCCESSED(m_mapMasterAssemblyList.Add(inAssemblyToken, spAssembly));
	}
	this->OnAssemblyAdded();
	
	return hr;


}

HRESULT XamlSchemaContext::AddAssemblyXmlnsDefinition(struct XamlAssemblyToken inAssembly, const CXString * pstrXmlnsUri, struct XamlTypeNamespaceToken inTypeNamespaceToken, const CXString * pstrTypeNamespace)
{
	HRESULT hr = S_OK;
	xshared_ptr<XamlSchemaContext> schemaContext;
	xshared_ptr<XamlAssembly> spAssembly;
	xshared_ptr<CXString> inXmlnsUri;
	xshared_ptr<XamlXmlNamespace> spXmlNamespace;
	xshared_ptr<CXString> ssNamespaceName;
	xshared_ptr<XamlTypeNamespace> spTypeNamespace;

	XASSERT_SUCCESSED(shared_from_this(schemaContext));


	XASSERT_SUCCESSED(m_mapMasterAssemblyList.Get(inAssembly, spAssembly));

	if(hr == S_FALSE)
	{
		XASSERT_SUCCESSED(E_FAIL);
	}


	XASSERT_SUCCESSED(NormalizeUsingXmlNs(*pstrXmlnsUri, inXmlnsUri));


	XASSERT_SUCCESSED(m_mapUriToXmlNamespace.Get(*inXmlnsUri, spXmlNamespace));

	if(hr == S_FALSE)
	{
		XASSERT_SUCCESSED(FlushXamlNodeStreamCacheManager());
		XASSERT_SUCCESSED(XamlXmlNamespace::Create(schemaContext, inXmlnsUri, m_xmlNamespaceVector.size(), spXmlNamespace));
		XASSERT_SUCCESSED(m_xmlNamespaceVector.push_back(const_pointer_cast<XamlNamespace>(spXmlNamespace)));
		XASSERT_SUCCESSED(m_mapUriToXmlNamespace.Add(*inXmlnsUri, spXmlNamespace));

	}

	XASSERT_SUCCESSED(ssNamespaceName.init(pstrTypeNamespace->Clone(TRUE,FALSE)));
	if(m_bAllowXmlnsOnlyReset)
	{

		XASSERT_SUCCESSED(GetXamlTypeNamespace(inTypeNamespaceToken, ssNamespaceName, spAssembly, spTypeNamespace));
	}
	else
	{

		XASSERT_SUCCESSED(XamlTypeNamespace::Create(schemaContext, ssNamespaceName, inTypeNamespaceToken, spAssembly, spTypeNamespace));
	}

	XASSERT_SUCCESSED(spXmlNamespace->AddTypeNamespace(spTypeNamespace));

	return hr;

}

HRESULT XamlSchemaContext::ResolvePropertyName(struct XamlTypeToken tokType, const CXString & pstrPropertyName, struct XamlPropertyToken & rtokProperty, struct XamlTypeToken & rtokPropertyType)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetDeclaringType(struct XamlPropertyToken tokProperty, struct XamlTypeToken & tokDeclaringType)
{

	NOTIMPL

}

XBOOL XamlSchemaContext::DoesAllowXmlnsOnlyReset()
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetSourceAssembly(xshared_ptr<CXString> & outssSourceAssembly)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::PushSourceAssembly(const xshared_ptr<CXString> & inssSourceAssembly)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::PopSourceAssembly()
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetXamlNamespace(const xshared_ptr<CXString> & inXmlns, const xshared_ptr<XamlAssembly> & outAssembly, xshared_ptr<XamlNamespace> & outNamespace)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetXamlNamespace(const xshared_ptr<CXString> & inXmlns, xshared_ptr<XamlNamespace> & outNamespace)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetXamlNamespaceFromRuntimeIndex(XUINT32 uiIndex, xshared_ptr<XamlNamespace> & spXamlNamespace)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::get_StringXamlType(xshared_ptr<XamlType> & spStringType)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::get_Int32XamlType(xshared_ptr<XamlType> & spInt32Type)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::get_StringSyntax(xshared_ptr<XamlTextSyntax> & outStringSyntax)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::get_Int32Syntax(xshared_ptr<XamlTextSyntax> & outInt32Syntax)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::get_PositionalParametersProperty(xshared_ptr<XamlProperty> &)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::get_InitializationProperty(xshared_ptr<XamlProperty> & outInitializationProperty)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetXamlDirective(const xshared_ptr<XamlNamespace> & inXamlNamespace, const xshared_ptr<CXString> inssName, xshared_ptr<XamlProperty> & outXamlProperty)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::ItemsProperty(const xshared_ptr<XamlType> & inCollectionType, xshared_ptr<XamlProperty> & outItemsProperty)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::get_SupportMarkupExtensionsWithDuplicateArity(XBOOL *)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetXamlAssembly(const struct XamlAssemblyToken & inAssemblyToken, xshared_ptr<XamlAssembly> & outXamlAssembly)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetXamlAssembly(const struct XamlAssemblyToken & inAssemblyToken, const xshared_ptr<CXString> & inAssemblyName, xshared_ptr<XamlAssembly> & outXamlAssembly)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetXamlTextSyntax(const struct XamlTypeToken & sTypeToken, xshared_ptr<XamlTextSyntax> & outXamlTextSyntax)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetXamlType(const struct XamlTypeToken & inTypeToken, const xshared_ptr<XamlNamespace> & inNamespace, xshared_ptr<XamlType> & outXamlType)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetXamlType(const struct XamlTypeToken & inTypeToken, const xshared_ptr<XamlNamespace> & inNamespace, const xshared_ptr<CXString> inTypeName, xshared_ptr<XamlType> & outXamlType)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetXamlType(const struct XamlTypeToken & inTypeToken, xshared_ptr<XamlType> & outXamlType)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetXamlTypeFromRuntimeIndex(XUINT32 uiIndex, xshared_ptr<XamlType> & spXamlType)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::CheckPeerType(const xshared_ptr<XamlQualifiedObject> & inQO, const xshared_ptr<CXString> & inssClassName, XBOOL bIsExactMatchRequired)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetXamlProperty(const xshared_ptr<CXString> & spFullyQualifiedPropertyName, xshared_ptr<XamlProperty> & outXamlProperty)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetXamlProperty(const struct XamlPropertyToken inPropertyToken, const struct XamlTypeToken inPropertyTypeToken, xshared_ptr<XamlProperty> & outXamlProperty)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetXamlProperty(const struct XamlPropertyToken inPropertyToken, const struct XamlTypeToken inPropertyTypeToken, const xshared_ptr<CXString> & inPropertyName, xshared_ptr<XamlProperty> & outXamlProperty)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetXamlProperty(const struct XamlPropertyToken inPropertyToken, xshared_ptr<XamlProperty> & outXamlProperty)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::RegisterXamlProperty(const xshared_ptr<XamlProperty> & spXamlProperty)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetXamlPropertyFromRuntimeIndex(unsigned int uiIndex, xshared_ptr<XamlProperty> & spXamlProperty)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetXamlTypeNamespace(const struct XamlTypeNamespaceToken inTypeNamespaceToken, const xshared_ptr<CXString> & inTypeNamespaceName, const xshared_ptr<XamlAssembly> & inXamlAssembly, xshared_ptr<XamlTypeNamespace> & outXamlTypeNamespace)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetTypeInfoProvider(enum XamlTypeInfoProviderKind tpk, xshared_ptr<XamlTypeInfoProvider> & outTypeInfoProvider)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetRuntime(enum XamlFactoryKind fk, xshared_ptr<XamlRuntime> & rspXamlRuntime)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetCore(CCoreServices * * ppCore)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetErrorService(xshared_ptr<ParserErrorReporter> & outErrorService)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::SetErrorService(const xshared_ptr<ParserErrorReporter> & inErrorService)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::SetFoundIgnoredUsingXmlns()
{

	NOTIMPL

}

HRESULT XamlSchemaContext::OnAssemblyAdded()
{

	NOTIMPL

}

HRESULT XamlSchemaContext::CrackXmlns(XUINT32 cXmlns, const XCHAR * pXmlns, XCHAR * * ppClrNamespace, XCHAR * * ppAssembly)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::EnsureAllXmlnsMappingsLoaded()
{

	NOTIMPL

}

HRESULT XamlSchemaContext::GetXmlnsMappings(xshared_ptr<XamlAssembly> &)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::NormalizeUsingXmlNs(const CXString & rstrXmlNs, xshared_ptr<CXString> & outNormalizedXmlNs)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::ExtractTypeNamespace(CCoreServices * pCore, const CXString & rstrXmlNs, xshared_ptr<CXString> & outTypeNamespace)
{
	HRESULT hr = S_OK;
	xshared_ptr<CXString> spTypeNamespace;
	XASSERT_SUCCESSED(spTypeNamespace.init(rstrXmlNs.Clone(TRUE,FALSE)));

	XASSERT_FAILED_GOTO(spTypeNamespace.init(rstrXmlNs.Clone(TRUE,FALSE)),CleareXIT);

	if(spTypeNamespace->StartsWith(new CXString(LEN_STR_PAIR(L"using:")),xstrCompareCaseSensitive))
	{

		hr = spTypeNamespace->Remove(0,STR_LEN(L"using:"));
	}

	outTypeNamespace = spTypeNamespace;
CleareXIT:
	if(hr < 0)
		outTypeNamespace.reset();
	return hr;
}

HRESULT XamlSchemaContext::InitializeXmlNamespaceMap(XBOOL bPreserveExistingSpecialNamespaces)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::FlushXamlNodeStreamCacheManager()
{
	HRESULT hr = S_OK;

	xshared_ptr<XamlNodeStreamCacheManager> spXamlNodeStreamCacheManager;

	XASSERT_SUCCESSED(m_pCore->GetXamlNodeStreamCacheManager(spXamlNodeStreamCacheManager));

	XASSERT_SUCCESSED(spXamlNodeStreamCacheManager->Flush());

	return hr;

}

HRESULT XamlSchemaContext::GetFullyQualifiedNameParts(const xshared_ptr<CXString> & spFullyQualifiedName, xshared_ptr<CXString> & outNamespaceUri, xshared_ptr<CXString> & outTypeName, xshared_ptr<CXString> & outPropertyName)
{

	NOTIMPL

}

HRESULT XamlSchemaContext::Initialize()
{

	NOTIMPL

}
